package com.aaa.service.impl;

import com.aaa.dao.TRoleMapper;
import com.aaa.dao.TuserMapper;
import com.aaa.entity.LayData;
import com.aaa.entity.MyPage;
import com.aaa.entity.TRole;
import com.aaa.entity.User;
import com.aaa.service.TRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (TRole)表服务实现类
 *
 * @author makejava
 * @since 2022-10-21 16:41:11
 */
@Service
public class TRoleServiceImpl implements TRoleService {
   @Autowired
   private TRoleMapper mapper;
   @Autowired
   private TuserMapper mapper1;

   @Override
   public boolean add(TRole trole) {
      int insert = -1;
      if (trole.getId() == null) {
         trole.getRname();
         insert = mapper.insert(trole);
      } else {
         //updateByPrimaryKeySelectiv为接收的参数为对应于数据库的实体类对象，
         //利用字段的自动匹配进行更新表的操作，如果传入obj对象中的某个属性值为null，则不进行数据库对应字段的更新。
         insert = mapper.updateByPrimaryKeySelective(trole);
      }
      return insert >= 0 ? true : false;
   }

   @Override
   public LayData query(MyPage myPage) {
      PageHelper.startPage(myPage.getPage(), myPage.getLimit());

      PageInfo pageInfo = new PageInfo(mapper.selectAll());

      return new LayData(pageInfo);
   }

   @Override
   public Boolean delete(TRole trole) {
      List<User> users = mapper1.selectfindAll(trole);
      if (users.size() == 0) {
         Integer delete = mapper.delete(trole);
         return true;
      }
      return false;

   }

   @Override
   public PageInfo findAll(MyPage myPage) {
      PageHelper.startPage(myPage.getPage(), myPage.getLimit());
      return new PageInfo(mapper.selectAll());
   }

   @Override
   public LayData queryById(String rid) {
      TRole trole = mapper.selectByPrimaryKey(rid);
      return new LayData(trole.getRname());
   }

   @Override
   public LayData queryId(String rid) {

      List<TRole> selectid = mapper.selectid(rid);
      System.out.println(selectid+"/*/*/*/*");
      return new LayData(selectid.get(0).getRname());
   }
}